উদাহরণ সহ WebClient ব্যবহার

Java Technologies - স্প্রিং বুট ক্লায়েন্ট (Spring Boot Client) WebClient এর পরিচিতি |
67
67

Spring Boot WebClient একটি অত্যাধুনিক HTTP ক্লায়েন্ট, যা Spring WebFlux ফ্রেমওয়ার্কের অংশ। এটি অ্যাসিনক্রোনাস এবং রিঅ্যাকটিভ প্রোগ্রামিংয়ের জন্য উপযুক্ত। নিচে উদাহরণসহ WebClient ব্যবহার সম্পর্কে বিস্তারিত আলোচনা করা হয়েছে।


১. WebClient কনফিগার করা

Spring Boot প্রজেক্টে WebClient ব্যবহার করতে চাইলে আগে spring-boot-starter-webflux ডিপেন্ডেন্সি যুক্ত করুন।

Maven Dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

WebClient Bean Configuration:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;

@Configuration
public class WebClientConfig {

    @Bean
    public WebClient webClient(WebClient.Builder builder) {
        return builder
                .baseUrl("https://api.example.com")
                .build();
    }
}

২. WebClient দিয়ে HTTP কল

২.১. GET রিকোয়েস্ট উদাহরণ:

import org.springframework.web.reactive.function.client.WebClient;

public class WebClientGetExample {
    private final WebClient webClient;

    public WebClientGetExample(WebClient webClient) {
        this.webClient = webClient;
    }

    public String fetchData() {
        return webClient.get()
                .uri("/data")
                .retrieve()
                .bodyToMono(String.class) // Response body as String
                .block(); // Blocking call (for synchronous)
    }

    public static void main(String[] args) {
        WebClient webClient = WebClient.builder().baseUrl("https://api.example.com").build();
        WebClientGetExample example = new WebClientGetExample(webClient);
        String response = example.fetchData();
        System.out.println("Response: " + response);
    }
}

২.২. POST রিকোয়েস্ট উদাহরণ:

import org.springframework.web.reactive.function.client.WebClient;

public class WebClientPostExample {
    private final WebClient webClient;

    public WebClientPostExample(WebClient webClient) {
        this.webClient = webClient;
    }

    public String sendData(MyRequestBody requestBody) {
        return webClient.post()
                .uri("/data")
                .bodyValue(requestBody)
                .retrieve()
                .bodyToMono(String.class)
                .block();
    }

    public static void main(String[] args) {
        WebClient webClient = WebClient.builder().baseUrl("https://api.example.com").build();
        WebClientPostExample example = new WebClientPostExample(webClient);
        MyRequestBody requestBody = new MyRequestBody("value1", "value2");
        String response = example.sendData(requestBody);
        System.out.println("Response: " + response);
    }
}

class MyRequestBody {
    private String field1;
    private String field2;

    // Constructors, Getters, and Setters
    public MyRequestBody(String field1, String field2) {
        this.field1 = field1;
        this.field2 = field2;
    }

    public String getField1() {
        return field1;
    }

    public void setField1(String field1) {
        this.field1 = field1;
    }

    public String getField2() {
        return field2;
    }

    public void setField2(String field2) {
        this.field2 = field2;
    }
}

২.৩. PUT এবং DELETE রিকোয়েস্ট:

PUT রিকোয়েস্ট:
webClient.put()
        .uri("/data/123")
        .bodyValue(new MyRequestBody("updatedValue1", "updatedValue2"))
        .retrieve()
        .bodyToMono(Void.class)
        .block();
DELETE রিকোয়েস্ট:
webClient.delete()
        .uri("/data/123")
        .retrieve()
        .bodyToMono(Void.class)
        .block();

৩. Error Handling

WebClient-এ কাস্টম এরর হ্যান্ডলিং:

webClient.get()
        .uri("/data")
        .retrieve()
        .onStatus(status -> status.is4xxClientError(), 
                  clientResponse -> Mono.error(new RuntimeException("Client Error!")))
        .onStatus(status -> status.is5xxServerError(), 
                  clientResponse -> Mono.error(new RuntimeException("Server Error!")))
        .bodyToMono(String.class)
        .block();

৪. Token বা Header যোগ করা (Authorization)

WebClient webClient = WebClient.builder()
        .baseUrl("https://api.example.com")
        .defaultHeader("Authorization", "Bearer your-token-here")
        .build();

String response = webClient.get()
        .uri("/protected-resource")
        .retrieve()
        .bodyToMono(String.class)
        .block();
System.out.println("Response: " + response);

৫. Reactive Programming Example

Flux ব্যবহার করে ডেটার স্ট্রিম ফ্লো পরিচালনা:

import reactor.core.publisher.Flux;

public void fetchDataStream() {
    Flux<String> dataStream = webClient.get()
            .uri("/stream")
            .retrieve()
            .bodyToFlux(String.class);

    dataStream.subscribe(data -> System.out.println("Received: " + data));
}

৬. Configuration in application.properties

Default Timeout সেটআপ:

webclient.default.read-timeout=5000
webclient.default.connect-timeout=3000

৭. Sample Controller Integration

Spring Boot Controller-এ WebClient ব্যবহারের উদাহরণ:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class WebClientController {
    private final WebClient webClient;

    public WebClientController(WebClient webClient) {
        this.webClient = webClient;
    }

    @GetMapping("/fetch-data")
    public String fetchData() {
        return webClient.get()
                .uri("/data")
                .retrieve()
                .bodyToMono(String.class)
                .block();
    }
}

সংক্ষেপে WebClient ব্যবহার:

  • সিনক্রোনাস (block) এবং অ্যাসিনক্রোনাস (reactive) উভয় প্রোগ্রামিং মডেল সাপোর্ট করে।
  • রিঅ্যাকটিভ প্রোগ্রামিংয়ের জন্য Flux এবং Mono ব্যবহার।
  • কাস্টম হেডার, টাইমআউট, এবং এরর হ্যান্ডলিং সহজে কনফিগার করা যায়।
  • RestTemplate-এর তুলনায় আরও আধুনিক এবং ফিচার-সমৃদ্ধ।
Content added By
টপ রেটেড অ্যাপ

স্যাট অ্যাকাডেমী অ্যাপ

আমাদের অল-ইন-ওয়ান মোবাইল অ্যাপের মাধ্যমে সীমাহীন শেখার সুযোগ উপভোগ করুন।

ভিডিও
লাইভ ক্লাস
এক্সাম
ডাউনলোড করুন
Promotion